package com.houseliker.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.houseliker.R;
import com.houseliker.listener.OnHeaderClickListener;

/**
 * Created by sunrx on 2015/12/7.
 */
public class IHeaderLayout extends RelativeLayout {

    private View header_bar;
    private LinearLayout ll_common_left;
    private ImageView iv_common_left;
    private TextView tv_common_left;
    private TextView tv_common_title;
    private TextView tv_common_right;

    private LayoutInflater layoutInflater;

    private static final int TYPE_HEADER = 1;

    private OnHeaderClickListener mListener;

    public IHeaderLayout(Context context, int layoutResourceId, int type) {
        super(context);
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setHeaderLayout(context, layoutResourceId, type);
    }

    public IHeaderLayout(Context context, LayoutInflater inflater, int layoutResourceId, int type) {
        super(context);
        layoutInflater = inflater;
        setHeaderLayout(context, layoutResourceId, type);
    }

    private void setHeaderLayout(Context context, int layoutResourceId, int type) {
        switch (type) {
            case TYPE_HEADER:
                addHeaderBar();
                break;
            default:
                break;
        }

        View view = layoutInflater.inflate(layoutResourceId, null);
        LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        params.addRule(RelativeLayout.BELOW, R.id.root_header_bar);

        addView(view, params);
    }

    public void setHeaderClickListener(OnHeaderClickListener listener) {
        mListener = listener;
    }

    protected void addHeaderBar() {
        header_bar = layoutInflater.inflate(R.layout.header_bar, null);
        ll_common_left = (LinearLayout) header_bar.findViewById(R.id.ll_common_left);
        iv_common_left = (ImageView) header_bar.findViewById(R.id.iv_common_left);
        tv_common_left = (TextView) header_bar.findViewById(R.id.tv_common_left);
        tv_common_title = (TextView) header_bar.findViewById(R.id.tv_common_title);
        tv_common_right = (TextView) header_bar.findViewById(R.id.tv_common_right);

        ll_common_left.setOnClickListener(onClicker);
        tv_common_right.setOnClickListener(onClicker);

        LayoutParams params = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        //居顶。
//        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        addView(header_bar, params);
    }

    private OnClickListener onClicker = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ll_common_left:
                    mListener.onClickLeftButton();
                    break;
                case R.id.tv_common_right:
                    mListener.onClickRightButton();
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * 显示“返回”按钮和title
     *
     * @param titleText
     */
    public void setHeaderBar(String titleText) {
        //显示返回按钮
        iv_common_left.setVisibility(VISIBLE);
        tv_common_left.setVisibility(VISIBLE);
        tv_common_title.setVisibility(VISIBLE);
        tv_common_right.setVisibility(GONE);

        tv_common_title.setText(titleText);
    }

    /**
     * 显示“返回”按钮、title、右边按钮。
     *
     * @param titleText
     * @param rightText
     */
    public void setHeaderBar(String titleText, String rightText) {
        iv_common_left.setVisibility(VISIBLE);
        tv_common_left.setVisibility(VISIBLE);
        tv_common_title.setVisibility(VISIBLE);
        tv_common_right.setVisibility(VISIBLE);

        tv_common_right.setText(rightText);
        tv_common_title.setText(titleText);
    }

    /**
     * 显示左边按钮、title、右边按钮
     *
     * @param leftText
     * @param titleText
     * @param rightText
     */
    public void setHeaderBar(String leftText, String titleText, String rightText) {
        iv_common_left.setVisibility(VISIBLE);
        tv_common_left.setVisibility(VISIBLE);
        tv_common_title.setVisibility(VISIBLE);
        tv_common_right.setVisibility(VISIBLE);

        tv_common_left.setText(leftText);
        tv_common_right.setText(rightText);
        tv_common_title.setText(titleText);
    }

    public String getTitle() {
        if (tv_common_title != null) {
            return tv_common_left.getText().toString();
        }
        return "";
    }

    /**
     * 隐藏左边箭头
     */
    public void hideHeaderLeftArrow() {
        iv_common_left.setVisibility(GONE);
    }

    /**
     * 隐藏左边按钮
     */
    public void hideHeaderLeftButtom() {
        iv_common_left.setVisibility(GONE);
        tv_common_left.setVisibility(GONE);
    }
}
